﻿<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
         <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="0" />    
    <title>供应商列表</title>
</head>
<body>
   
    <div class="container-fluid">
        <div class="line20"></div>    
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-add">新增供应商</button>
        <div class="line10"></div>
          
        <table class="table table-striped table-hover" border="0" cellpadding="0" cellspacing="0"  id="userTable">
            <thead>
                <tr>
                    <th width="40">ID</th>
                    <th width="200">供应商名称</th>
                    <th width="100">联系人</th>
                    <th width="120">手机</th>
                    <th width="100">电话</th> 
                     <th width="200">地址</th>   
                     <th  class="u-remark">备注</th>  
                    <th width="100">操作</th>                            
                </tr>
            </thead>
            <tbody>
                <td colspan="8">数据加载中……</td>
            </tbody>
        </table>

      <div class="m-pager">
            <div class="u-pager-info"></div>
            <form class="form-inline m-pager-jump" role="form">
                <div class="form-group">
                    <label for="jump">跳转到</label>
                    <input type="number" class="form-control number" id="jump" name="jump" placeholder="输入页码">
                </div>
                <button type="submit" class="btn btn-ynbm btn-sm">跳转</button>
            </form>
            <div class="btn-group m-pager-btn-group">
                <button type="button" class="btn" id="firstbtn">首页</button>
                <button type="button" class="btn" id="prevbtn">上一页</button>
                <button type="button" class="btn" id="nextbtn">下一页</button>
                <button type="button" class="btn" id="lastbtn">尾页</button>
            </div>
        </div>
    </div>

      <!--新增 -->
    <div class="modal fade" id="modal-add">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">添加供应商</h4>
                </div>  
                  <form id="form1">
                <div class="modal-body">
                    <div class="m-add-form">
                        <input type="hidden" name="adduserid" id="adduserid">
                        <div class="m-add-1">
                        <input type="hidden" id="hidid" name="hidid" />

                             <div class="row">                                
                                <div class="form-group col-xs-6">
                                    <label>公司名称</label>
                                    <div class="controls">
                                    <input type="text" class="form-control"  id="title" name="title"  datatype="*2-20" >
                                    </div>
                                </div>
                                  <div class="form-group col-xs-6">
                                    <label>详细地址</label>
                                    <div class="controls">
                                    <input type="text" class="form-control" id="addr" name="addr" >  
                                    </div>                              
                                </div>
                                </div>
                                 <div class="row">
                                        <div class="form-group col-xs-4">                             
                                    <label>联系人</label>
                                    <div class="controls">
                                    <input type="text" class="form-control" id="contact" name="contact" >
                                    </div>
                                </div>
                                 <div class="form-group col-xs-4">
                                    <label>手机</label>
                                    <div class="controls">
                                    <input type="text" class="form-control" id="phone" name="phone" datatype="m" >
                                    </div>
                                </div>
                              
                                 <div class="form-group col-xs-4">
                                    <label>电话</label>
                                    <div class="controls">
                                    <input type="text" class="form-control"  id="tel" name="tel" >
                                    </div>
                                </div>
                                     </div>
                              
                                <div class="form-group">
                                    <label>备注</label>
                                    <textarea class="form-control"  rows="3" id="remark" name="remark" ></textarea>
                                </div>
                         
                           
                         
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary" >提交保存</button>
                </div>
                   </form>
            </div>
        </div>
    </div>

   
<link rel="stylesheet" type="text/css" href="/admin/res/style.css">
<link rel="stylesheet" type="text/css" href="/admin/res/index.css">
<script src="/admin/res/jquery.min.js"></script>
<script src="/skin/scripts/bs.min.js"></script>
<script src="/admin/res/cookie.js"></script>
<script src="/admin/res/layindex.js"></script>
<script type="text/javascript" src="/skin/scripts/Validform_v5.3.2_min.js"></script>
    <script type="text/javascript">    
          
       

        $(document).ready(function () {

            var totalpages, totalrecords, pageindex, pagesize, startindex
            ajaxData = {};
            //加载详细列表
            queryUserInfo = function (pageIndex) {              
                ajaxData.pageindex = 1;
                ajaxData.pagesize = 10;
                if (pageIndex && /^\d*$/.test(pageIndex) && pageIndex > 0) {
                    ajaxData.pageindex = pageIndex;
                }
                $.ajax({
                    url: '/cms/supply/jsonlist',
                    type: 'post',
                    dataType: 'json',
                    data:ajaxData
                })
                .done(function (data) {
                	 if (data.length == 0) {  
                		 $('#userTable tbody').html('<tr><td colspan=8>无数据</td></tr>');
                		 return;                        
                     }
                    var index, tb = $('<tbody></tbody>');
                    totalpages = data.totalpages * 1;
                    totalrecords = data.totalrecords * 1;
                    pageindex = data.pageindex * 1;
                    pagesize = data.pagesize * 1;
                    startindex = (pageindex - 1) * pagesize + 1;
                    console.log("---->length:"+data.length)
                   
         
                    for (var i = 0; i < data.length; i++) {
                        index = startindex + i;
                        tb.append($('<tr></tr>')
                            .append('<td>' + data[i].id + '</td>')
                             .append('<td>' + data[i].title + '</td>')
  
                            .append('<td>' + data[i].contact + '</td>')
                              .append('<td>' + data[i].phone + '</td>')
                              .append('<td>' + data[i].tel + '</td>')                            
                            .append('<td>' + data[i].addr + '</td>')
                              .append('<td  class="u-remark">' + data[i].remark + '</td>')
                             .append($('<td></td>')
                                         .append($('<a id="edit" >修改</a>')
                                            .data('content', data[i])
                                        )
                                         .append($('<a id="del">&nbsp;&nbsp; 删除</a>')
                                            .data('content', data[i])
                                        ))                             
                               
                        );
                    }
                    $('#userTable tbody').replaceWith(tb);
                    $('#jump').val(pageindex);
                    $('.u-pager-info').html('共' + totalrecords + '条记录，每页' + pagesize + '条，共' + totalpages + '页，现在是第' + pageindex + '页');

                });
            };

            queryUserInfo();
          

            //跳转到第几页
            $('.m-pager-jump').submit(function (event) {
                event.preventDefault();
                var jump = $('#jump').val();
                if (jump && /^\d*$/.test(jump) && jump > 0) {
                    if (jump <= totalpages) {
                        queryUserInfo(jump);
                    } else {
                        alert('总共有' + totalpages + '页');
                    }
                } else {
                    alert('页数必须为正整数');
                }
            });
            $('#firstbtn').click(function (event) {
                queryUserInfo(1);
            });
            $('#prevbtn').click(function (event) {
                if (pageindex > 1) {
                    queryUserInfo(pageindex - 1);
                } else {
                    queryUserInfo(1);
                }
            });
            $('#nextbtn').click(function (event) {
                if (pageindex < totalpages) {
                    queryUserInfo(pageindex + 1);
                } else {
                    queryUserInfo(totalpages);
                }
            });
            $('#lastbtn').click(function (event) {
                queryUserInfo(totalpages);
            });

             $('#firstcatelog li').click(function (event) {
                 load(this);
             });

            //新增初始化
             $('#modal-add').on('show.bs.modal', function (event) {
                 var modal = $(this);
                 modal.find('#hidid').val('');
                 modal.find('#title').val('');
                 modal.find('#contact').val('');
                 modal.find('#phone').val('');
                 modal.find('#tel').val('');
                 modal.find('#addr').val('');
                 modal.find('#remark').val('');
             });


            //新增保存
             $("#form1").Validform({
                 tiptype: 3,
                 callback: function(form) {

                     var modal = $("#modal-add");
                     var id = modal.find('#hidid').val();
                 var title = modal.find('#title').val();
                 var contact = modal.find('#contact').val();
                 var phone = modal.find('#phone').val();
                 var tel = modal.find('#tel').val();
                 var addr = modal.find('#addr').val();
                 var remark = modal.find('#remark').val();
                 var data = {title: title, contact: contact, phone: phone, tel: tel, addr: addr, remark: remark };
                 if (id!="") {                    
                     data._method="put";
                     data.id=id;
                 }                
                 ajaxurl("/cms/supply/jsonadd",'post', data, function success(data) {
                     if (data.status == 1) {
                         modal.modal('hide');
                         alert('操作成功');
                         queryUserInfo();
                     }
                     else
                         alert(data.msg);
                 });
                 return false;
                 ajaxNow.abort();
                 }
             });

            //修改初始化
             $("#userTable").on("click", "#edit", {}, function (event) {
                 var modal = $('#modal-add'),                     
                     content = $(this).data('content');
                 modal.modal('show');
                 modal.find('#hidid').val(content.id);
                 modal.find('#title').val(content.title);
                 modal.find('#contact').val(content.contact);
                 modal.find('#phone').val(content.phone);
                 modal.find('#tel').val(content.tel);  
                 modal.find('#addr').val(content.addr);
                 modal.find('#remark').val(content.remark);                 

             });

            //提示删除
             $("#userTable").on("click", "#del", {}, function (event) {
                 var content = $(this).data('content');
                 if (window.confirm('确定删除 ' + content.title + ' ？')) {
                     ajaxurl('/cms/supply/jsondel/'+content.id, 'post', {_method:'delete'}, function success(data) {
                         if (data.status == 1) {                            
                             queryUserInfo();
                         }
                         else
                             alert(data.msg);
                     });

                 }
                 else
                     return false;

             });

        });
    </script>
</body>
</html>
